package com.yaopaikeji.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {

    public static final String FORMAT_STYLE_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_STYLE_YMD = "yyyy-MM-dd";

    private static DateFormat df = new SimpleDateFormat(FORMAT_STYLE_YMDHMS);
    private static DateFormat dfNoTime = new SimpleDateFormat(FORMAT_STYLE_YMD);

    public static Date now(){

        return new Date();
    }

    public static String formate(Date date){
        if(date == null)
            return null;

        return df.format(date);
    }

    public static String formate(Date date,String pattern){

        if(date == null)
            return null;

        if(pattern == null || pattern.isEmpty())
            return df.format(date);

        return new SimpleDateFormat(pattern).format(date);
    }

    public static String getCurrentTimeStr(){

        return df.format(now());
    }

    public static Date parseDate(String dateStr){

        try {
            return df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDateByPtn(String dateStr,String pattern){

        try {
            return new SimpleDateFormat(pattern).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
